<template>
  <div class="app-container">
    <!-- 顶部搜索栏 -->
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="" prop="keyword">
        <el-input
          v-model="queryParams.keyword"
          placeholder="请输入表单名称或者key"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-divider direction="vertical"></el-divider>
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddDraft">新增</el-button>
      </el-form-item>
    </el-form>

    <!-- 列表 -->
    <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" align="left" width="50"/>
      <el-table-column label="key" align="left" prop="formKey" width="150"/>
      <el-table-column label="标题" align="left" prop="formTitle" :show-overflow-tooltip="true" width="200"/>
      <el-table-column label="说明" align="left" prop="formDesc" :show-overflow-tooltip="true" width="300"/>
      <el-table-column label="创建人" align="left" prop="createBy" width="100"/>
      <el-table-column label="创建时间" align="left" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="left" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >编辑</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-takeaway-box"
            @click="handlePublish(scope.row)"
          >发布</el-button>

          <el-dropdown style="margin-left: 10px;">
            <el-button type="text" size="mini">
              更多操作<i class="el-icon-arrow-down el-icon--right"></i>
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="handleDelete(scope.row)" divided icon="el-icon-delete">删除</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>

        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="dialog.title" :visible.sync="dialog.open" width="50%" append-to-body>
      <el-form ref="dialog-form" :model="dialog.form" :rules="dialog.rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="脚本key" prop="formKey">
              <el-input v-model="dialog.form.formKey" placeholder="表单key"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="标题" prop="formTitle">
              <el-input v-model="dialog.form.formTitle" placeholder="请输入标题" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-col :span="12">
          <el-form-item label="版本" prop="grCategory">
            <el-input v-model="dialog.form.version" placeholder="" :readonly="true"/>
          </el-form-item>
        </el-col>
        <el-row>
          <el-col :span="12">
            <el-form-item label="分组" prop="category">
              <el-input v-model="dialog.form.category" placeholder="请输入分组" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="备注" prop="grDesc">
          <el-input v-model="dialog.form.formDesc" type="textarea" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
    import {listDraft, publishDraft, removeDraft} from "@/api/openform/generator";

    export default {
        name: "FormList",
        data() {
            return {
                loading: true,// 遮罩层
                ids: [],// 选中数组
                single: true,// 非单个禁用
                multiple: true,// 非多个禁用
                total: 0,// 总条数
                dataList: [],// 参数表格数据
                dialog: {
                    title: "",// 弹出层标题
                    open: false,// 是否显示弹出层
                    defaultForm: {// 默认表单
                        version: 1
                        , category: "默认分组"
                    },
                    form: {},
                    rules: {// 表单校验
                        formKey: [
                            { required: true, message: "key不能为空", trigger: "blur" }
                        ],
                        formTitle: [
                            { required: true, message: "标题不能为空", trigger: "blur" }
                        ],
                        category: [
                            { required: true, message: "分组不能为空", trigger: "blur" }
                        ]
                    }
                },
                queryParams: {// 查询参数
                    pageNum: 1,
                    pageSize: 10,
                    keyword: undefined
                },
            };
        },
        created() {//创建回调
            this.getList();//查询数据
        },
        methods: {
            // 查询数据
            getList() {
                this.loading = true;
                listDraft(this.queryParams).then(response => {
                    this.dataList = response.rows;
                    this.total = response.total;
                    this.loading = false;
                });
            },
            // 取消按钮
            cancel() {
                this.dialog.open = false;
                this.clearForm();
            },
            // 清空表单
            clearForm() {
                this.dialog.form = this.dialog.defaultForm;
                this.resetForm("dialog-form");
            },
            // 多选框选中数据
            handleSelectionChange(selection) {
                this.ids = selection.map(item => item.id)
                this.single = selection.length!=1
                this.multiple = !selection.length
            },
            /** 搜索按钮操作 */
            handleQuery() {
                this.queryParams.pageNum = 1;
                this.getList();
            },
            /** 重置按钮操作 */
            resetQuery() {
                this.resetForm("queryForm");
                this.handleQuery();
            },
            /** 新增按钮操作 */
            handleAdd() {
                this.clearForm();
                this.dialog.open = true;
                this.dialog.itle = "新增数据";
            },
            /** 修改基本信息 */
            handleUpdate(row) {
                const dataId = row.id || this.ids[0];
                this.$router.push({ path: "/openform/design/"+dataId });
            },
            /** 跳转页面 */
            handleShowForm(row) {
                const dataId = row.id || this.ids[0];
                this.$router.push({ path: "/openform/review/draft/"+dataId });
            },
            /** 跳转页面 */
            handleAddDraft() {
                this.$router.push("/openform/design");
            },
            /** 提交按钮 */
            submitForm: function() {
                this.$refs["dialog-form"].validate(valid => {
                    if (!valid) {//校验表单
                        return;
                    }
                    if (this.dialog.form.id) {//id不为空表示修改
                        /*save(this.form).then(res => {
                            this.msgSuccess("修改成功");
                            this.dialog.open = false;
                            this.getList();
                        });*/
                    } else {
                        /*save(this.form).then(res => {
                            this.msgSuccess("新增成功");
                            this.dialog.open = false;
                            this.getList();
                        });*/
                    }
                });
            },
            /** 删除按钮操作 */
            handleDelete(row) {
                const dataId = row.id || this.ids;
                this.$confirm('确认删除数据?', "警告", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                }).then(function() {
                    return removeDraft(dataId);
                }).then(() => {
                    this.getList();
                    this.msgSuccess("删除成功");
                })
            },
            /** 部署按钮操作 */
            handlePublish(row) {
                const dataId = row.id || this.ids;
                this.$confirm('确认发布表单数据?', "警告", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                }).then(function() {
                    return publishDraft(dataId);
                }).then(() => {
                    this.getList();
                    this.msgSuccess("发布成功");
                })
            },
        }
    };
</script>
<style>
</style>
